package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDao customerDao;


    //客户列表分页（名称模糊查询）
    @Override
    public Map<String, Object> list(Integer page, Integer rows, String customerName) {
        if (page==1){
            page=0;
        }
      List<Customer> customerList = customerDao.getList(page,rows,customerName);
        HashMap<String, Object> map = new HashMap<>();

        map.put("total", customerList.size());
        map.put("rows", customerList);
        return map;

    }

    //客户添加或修改
    @Override
    public void saveAndUpdate(Customer customer, Integer customerId) {
        if (customerId!=null){
            customerDao.deleteCustomer(customerId);
        }
        customerDao.saveCustomer(customer);
    }

    //客户删除（支持批量删除）
    @Override
    public void delete(String ids) {
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        customerDao.deleteAll(list);
    }
}
